import { Injectable } from '@nestjs/common';
import { Teacher } from '../teacher/entities/teacher.entity';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { Major } from '../major/entities/major.entity';
import { Role } from '../roles/entities/role.entity';
import { Department } from '../department/entities/department.entity';
import { ClassRoom } from '../class-rooms/entities/class-room.entity';
import { Course } from '../course/entities/course.entity';
import { TimeSlot } from '../time-slots/entities/time-slot.entity';

@Injectable()
export class CommonService {
  constructor(
    @InjectRepository(Course)
    private courseRepository: Repository<Course>,

    @InjectRepository(ClassRoom)
    private classRoomRepository: Repository<ClassRoom>,

    @InjectRepository(Teacher)
    private teacherRepository: Repository<Teacher>,

    @InjectRepository(Major)
    private majorRepository: Repository<Major>,

    @InjectRepository(Role)
    private roleRepository: Repository<Role>,

    @InjectRepository(Department)
    private departRepository: Repository<Department>,

    @InjectRepository(TimeSlot)
    private timersSlotRepository: Repository<TimeSlot>,
  ) {}

  findAllTeacher() {
    return this.teacherRepository
      .createQueryBuilder('teacher')
      .select('teacher.tid', 'key')
      .addSelect('teacher.tid', 'value')
      .addSelect('teacher.t_name', 'label')
      .getRawMany();
  }

  findAllMajor() {
    return this.majorRepository
      .createQueryBuilder('major')
      .select('major.id', 'key')
      .addSelect('major.id', 'value')
      .addSelect('major.name', 'label')
      .getRawMany();
  }

  findAllRole() {
    return this.roleRepository
      .createQueryBuilder('role')
      .select('role.id', 'key')
      .addSelect('role.id', 'value')
      .addSelect('role.name', 'label')
      .getRawMany();
  }

  findAllDepartment() {
    return this.departRepository
      .createQueryBuilder('department')
      .select('department.id', 'key')
      .addSelect('department.id', 'value')
      .addSelect('department.name', 'label')
      .getRawMany();
  }

  findAllClassRoom() {
    return this.classRoomRepository
      .createQueryBuilder('class-room')
      .select('class-room.id', 'key')
      .addSelect('class-room.id', 'value')
      .addSelect('class-room.class_room_name', 'label')
      .getRawMany();
  }

  findAllCourses() {
    return this.courseRepository
      .createQueryBuilder('course')
      .select('course.id', 'key')
      .addSelect('course.id', 'value')
      .addSelect('course.name', 'label')
      .getRawMany();
  }

  findAllTimeSlot() {
    return this.timersSlotRepository
      .createQueryBuilder('time_slot')
      .select('time_slot.timeSlot_id', 'key')
      .addSelect('time_slot.timeSlot_id', 'value')
      .addSelect('CONCAT(time_slot.start_date,"-",time_slot.end_date)', 'label')
      .where('not time_slot.isDel')
      .getRawMany();
  }
}
